import {Component, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {SeedInfiniteScroll} from 'app/shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component';
import {ToolsService} from "app/shared/service/tools/tools.service";
import {ActivatedRoute} from "@angular/router";

import {Subject} from "rxjs/Subject";
import {
  MultilayerScoreScanVO, MultilayerScoreVO, MultilayerScoreVOAlone,
  SmallScoreScanVO
} from "../../../../../interface";
import {MatchService} from "../../../match.service";




@Component({
  selector: 'match-score',
  templateUrl: './match-score.component.html',
  styleUrls: ['./match-score.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class MatchScoreComponent implements OnInit {
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  private ngUnsubscribe: Subject<any> = new Subject<any>();
  public personalInfo: MultilayerScoreVOAlone; // 个人信息
  public personalScore: Array<SmallScoreScanVO>; // 分数信息
  public groupInfo: MultilayerScoreVO; // 团体信息
  public mssvoList: Array<MultilayerScoreScanVO>; // 场次信息
  public params: any = {
    matchScheduleId: 0,
    isTeam: 0
  };
  public pageDate = {
    scheduleType: 0, // 赛程
    joinRequireType: 0, // 0:个人，1:双人
    isCup: false, // 是否杯赛
    matchType: 0, // 赛事类型
    hostUserName2: '',
    guestUserName2: '',
    isTempShowName: false // 是否显示临时名字
  };

  constructor(private toolsService: ToolsService,
              private service: MatchService,
              private route: ActivatedRoute) {
  }

  /**
   * 赛程比分查看(个人)
   */
  public getPersonalScore() {
    this.service.getAloneMultilayerScoreScan(this.params.matchScheduleId).takeUntil(this.ngUnsubscribe).subscribe(res => {
      this.toolsService.hideLoading();
      if (this.checkResult(res)) {
        this.personalInfo = res.data;
        this.personalScore = res.data.smallScoreScanVO;
        this.nameFormat(this.personalInfo);
      }
    });
  }

  /**
   * 赛程比分查看(团体)
   */
  public getGroupScore() {
    this.service.getMultilayerScoreScan(this.params.matchScheduleId).takeUntil(this.ngUnsubscribe).subscribe(res => {
      this.toolsService.hideLoading();
      if (this.checkResult(res)) {
        this.groupInfo = res.data;
        this.mssvoList = res.data.mssvo;
        this.mssvoList.filter((value) => {
          this.nameFormat(value);
        });
      }
    });
  }

  /**
   *检查返回状态
   */
  public checkResult(res) {
    if (res.result !== '0') {
      this.toolsService.showToast(res.msg);
    }
    return res.result === '0';
  }

  /**
   *检查参赛要求
   */
  public checkJoinRequire(code) {
    this.service.competitionStore.joinRequireArr.filter((value) => {
      if (code === value) {
        this.pageDate.joinRequireType = 1;
      }
    });
  }

  /**
   *格式化名字
   */
  private nameFormat(item) {
    const hostUserName = item.hostUserName;
    const guestUserName = item.guestUserName;

    if (hostUserName && hostUserName.indexOf(',') !== -1) {
      item.hostUserName = hostUserName.replace(',', '&');
    }

    if (guestUserName && guestUserName.indexOf(',') !== -1) {
      item.guestUserName = guestUserName.replace(',', '&');
    }
  }

  ngOnInit() {
    this.toolsService.showLoading();
    this.route.queryParams.subscribe(params => {
      this.params.matchScheduleId = params['matchScheduleId'];
      this.params.isTeam = Number(params['isTeam']);
      this.checkJoinRequire(Number(params['joinRequire']));
      this.pageDate.matchType = Number(params['matchType']);
      this.pageDate.scheduleType = Number(params['scheduleType']);
      this.pageDate.hostUserName2 = decodeURI(params['hostUserName2']);
      this.pageDate.guestUserName2 = decodeURI(params['guestUserName2']);
      if (this.params.isTeam === 1) {
        this.getGroupScore();
      } else {
        this.getPersonalScore();
      }

      this.service.competitionStore.cupCodeArr.filter((value) => {
        if (this.pageDate.scheduleType === value) {
          this.pageDate.isCup = true;
        }
      });

      if (this.pageDate.isCup && this.pageDate.joinRequireType === 1) {
        this.pageDate.isTempShowName = true;
      }
    });
  }
}
